package com.escape.auth.application.controller;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.alibaba.fastjson.JSON;
import com.escape.auth.application.convert.AuthRoleDTOConverter;
import com.escape.auth.application.convert.AuthUserDTOConverter;
import com.escape.auth.application.dto.AuthRoleDTO;
import com.escape.auth.application.dto.AuthUserDTO;
import com.escape.auth.common.entity.Result;
import com.escape.auth.domain.entity.AuthRoleBO;
import com.escape.auth.domain.entity.AuthUserBO;
import com.escape.auth.domain.service.AuthRoleDomainService;
import com.escape.auth.domain.service.AuthUserDomainService;
import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Author: escape_zebra
 * @CreateTime: 2025-01-07  20:42
 * @Description: TODO
 * @Version: 1.0
 */
@RestController
@RequestMapping("/role/")
@Slf4j
public class RoleController {

    @Resource
    private AuthRoleDomainService authRoleDomainService;

    /**
     * 新增角色
     */
    @RequestMapping("add")
    public Result<Boolean> add(@RequestBody AuthRoleDTO authRoleDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("RoleController.add.dto: {}",JSON.toJSONString(authRoleDTO));
            }
            Preconditions.checkArgument(!StringUtils.isBlank(authRoleDTO.getRoleName()), "角色名不能为空!");
            Preconditions.checkArgument(!StringUtils.isBlank(authRoleDTO.getRoleKey()), "key 不能为空!");

            AuthRoleBO authRoleBO = AuthRoleDTOConverter.INSTANCE.convertDtoToBo(authRoleDTO);

            return Result.ok(this.authRoleDomainService.add(authRoleBO));
        } catch (Exception e) {
            log.error("RoleController.add", e.getMessage(), e);
            return Result.fail("角色添加失败!");
        }
    }

    /**
     * 更新角色
     */
    @RequestMapping("update")
    public Result<Boolean> update(@RequestBody AuthRoleDTO authRoleDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("RoleController.update.dto: {}",JSON.toJSONString(authRoleDTO));
            }
            Preconditions.checkNotNull(authRoleDTO.getId(), "角色 id 不能为空!");
            Preconditions.checkArgument(!StringUtils.isBlank(authRoleDTO.getRoleName()), "角色名不能为空!");
            Preconditions.checkArgument(!StringUtils.isBlank(authRoleDTO.getRoleKey()), "key 不能为空!");

            AuthRoleBO authRoleBO = AuthRoleDTOConverter.INSTANCE.convertDtoToBo(authRoleDTO);

            return Result.ok(this.authRoleDomainService.update(authRoleBO));
        } catch (Exception e) {
            log.error("RoleController.update", e.getMessage(), e);
            return Result.fail("角色更新失败!");
        }
    }

    /**
     * 删除角色
     */
    @RequestMapping("delete")
    public Result<Boolean> delete(@RequestBody AuthRoleDTO authRoleDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("RoleController.delete.dto: {}",JSON.toJSONString(authRoleDTO));
            }
            Preconditions.checkNotNull(authRoleDTO.getId());

            AuthRoleBO authRoleBO = AuthRoleDTOConverter.INSTANCE.convertDtoToBo(authRoleDTO);

            return Result.ok(this.authRoleDomainService.delete(authRoleBO));
        } catch (Exception e) {
            log.error("RoleController.update", e.getMessage(), e);
            return Result.fail("角色更新失败!");
        }
    }
}
